home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / wgt / wgttut1 / fillpoly.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-07  |  3.3 KB  |  129 lines

  1. #include <dos.h>
  2.  
  3. /* Filled polygons by Chris Egerter
  4.    System independent version */
  5.  
  6. int startx[200];
  7. int endx[200];
  8.  
  9. typedef struct
  10.     {
  11.      int x,y;
  12.     } point;
  13.  
  14.  
  15. void polyline (int x1, int y1, int x2, int y2)
  16. /* Calculates the coordinates of a line given two
  17.    vertices (x1,y1) an (x2,y2).
  18.  
  19.    We will use fixed point math to speed things up.
  20.    The x coordinate is multiplied by 256 and for each row,
  21.    a constant m is added to x.  This is a simplified version
  22.    of a line algorithm because we only have to store 1 x coordinate
  23.    for every y coordinate.
  24.  
  25. */
  26. {
  27.  int tmp,y;
  28.  long x,m;
  29.  
  30.  if (y2 != y1)      /* This isn't a horizontal line */
  31.  {
  32.    if (y2 < y1)    /* Make sure y2 is greater than y1 */
  33.    {
  34.     tmp = y1;      /* Swap the y coordinate */
  35.     y1 = y2;
  36.     y2 = tmp;
  37.  
  38.     tmp = x1;      /* Swap the corresponding x coordinates */
  39.     x1 = x2;
  40.     x2 = tmp;
  41.    }
  42.  
  43.  x = (long)x1<<8;  /* Multiply be 256 */
  44.  
  45.  m = ((long)(x2 - x1)<<8) / ((long)(y2 - y1));
  46.  /* m is the fractional amount to add to the x coordinate every row.
  47.     m is equal to (delta x) / (delta y).  In other words, the x coordinate
  48.     has to change by (x2 - x1) columns in (y2 - y1) rows. */
  49.  
  50.  x += m; /* We ALWAYS skip the first point in every line. This is done */
  51.  y1++; /* because we do not want to store the point where two lines
  52.       meet, twice.  This would result in a single point being drawn. */
  53.  
  54.  for (y = y1; y <= y2; y++) /* Go through each row */
  55.   {
  56.    if ((y >= 0) & (y < 200)) /* If the coordinate is on the screen */
  57.     if (startx[y] == -16000) /* Store the first coordinate */
  58.       startx[y] = x>>8;
  59.     else
  60.       endx[y] = x>>8;        /* Store the last coordinate */
  61.    x += m;             /* Add our constant to x */
  62.    }
  63.  }
  64. }
  65.  
  66.  
  67.  
  68. void fillpoly (point *vertexlist, int numvertex)
  69. /* Draws a filled polygon given an array of vertices. */
  70. {
  71. int i;
  72. point *curpt,*nextpt;
  73.   /* Two pointers to a vertex. These are used to connect to vertices
  74.      together in when calling the polyline routine. */
  75.  
  76.  curpt = vertexlist;      /* Set to the first vertex in the array */
  77.  nextpt = vertexlist + 1; /* and to the second vertex */
  78.  
  79.  for (i = 0; i < 200; i++)
  80.   {
  81.    startx[i] = -16000;     /* Set up our impossible values */
  82.    endx[i] = -16000;
  83.   }
  84.  
  85.  for (i = 1; i < numvertex; i++)
  86.   {
  87.    polyline (curpt->x, curpt->y, nextpt->x, nextpt->y);
  88.    /* Calculate the edge of this line. */
  89.  
  90.    curpt += 1;  /* Go to the next line */
  91.    nextpt += 1;
  92.   }
  93.  
  94.   nextpt = vertexlist;  /* Now close the polygon by doing a line between
  95.                the first and last vertex. */
  96.   polyline (curpt->x, curpt->y, nextpt->x, nextpt->y);
  97.  
  98.   for (i = 0; i < 200; i++)   /* Now draw the horizontal line list */
  99.     if (startx[i] != -16000)  /* Indicates there is a line on this row */
  100.     {
  101.      if (endx[i] == -16000)
  102.      endx[i] = startx[i]; /* In case there was only one
  103.                  point found on this row */
  104.        line (startx[i], i, endx[i], i);
  105.        /* Draw a line between the two x coordinates, on the row i. */
  106.     }
  107. }
  108.  
  109.  
  110.  
  111. point mypoints[10];
  112.  
  113. void main (void)
  114. {
  115.  initialize_graphics ();
  116.  
  117.  mypoints[0].x = 160;
  118.  mypoints[0].y = 30;
  119.  mypoints[1].x = 50;
  120.  mypoints[1].y = 150;
  121.  mypoints[2].x = 270;
  122.  mypoints[2].y = 180;
  123.  
  124.  fillpoly (&mypoints, 3);
  125.  getch ();
  126.  close_graphics ();
  127. }
  128.  
  129.